package com.fyy.im.server.handler;

import com.fyy.im.message.ChatRequestMessage;
import com.fyy.im.message.ChatResponseMessage;
import com.fyy.im.server.session.SessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @Author fyy
 * @Description
 * @Date 2021/5/12 14:50
 */
@ChannelHandler.Sharable
public class ChatRequestMessageHandler extends SimpleChannelInboundHandler<ChatRequestMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ChatRequestMessage msg) throws Exception {
        String to = msg.getTo();
        Channel channel = SessionFactory.getSession().getChannel(to);
        if(null != channel){ //用户在线
            channel.writeAndFlush(new ChatResponseMessage(msg.getFrom(),msg.getContent()));
        }else{ //用户不在线
            ctx.writeAndFlush(new ChatResponseMessage(false,"对方不在服务区！"));
        }
    }

}
